****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************
	
clear all
set more off

global lotcons "female black white hispanic lunch?? lunch_miss app2 app3 anyt109 anyt110 anyt111 anyt112 anyt1lunch?? esly??"  

global nccons "math_lag read_lag science_lag math_gr_lag read_gr_lag mardgr_int_lag science_gr_lag allgr_int_lag i.lowread_nonmag#i.year lep_Y_lag ma_aig_lag rd_aig_lag aig_int_lag"
	
use all-lottery-movers-analysis.dta , clear 
keep if (year>2008 & ksample & stayer==1)

gen frl_diff = frl_mb_at - frl_mb_as
gen white_diff = white_mb_at - white_mb_as
gen aig_diff = aig_at - aig_as
gen lep_diff = lep_Y_at - lep_Y_as
gen schoolva_diff = schoolva_at - schoolva_as
gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_at - avgtestscores_as

gen notmove1 = (1 - move1)
gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1
gen move1_lost = (1 - wonfirst)*move1
gen notmove1_lost = notmove1*(1 - wonfirst)
gen attappch1schl = (curr_lead1==appch1school)
gen attassschl = (curr_lead1==assschl)
gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier
gen move1_win_noncomp = win_noncomplier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1

xtset lottery

global outcomecats "move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp"

xtreg avgscore_diff $outcomecats $lotcons , fe cluster(lottery) 

su avgscore_diff 
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store main

xtreg schoolva_diff $outcomecats $lotcons , fe cluster(lottery) 

estadd scalar N_ind = e(N)

su schoolva_diff
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

est store A

xtreg frl_diff $outcomecats $lotcons , fe cluster(lottery)

su frl_diff
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store B

xtreg aig_diff $outcomecats $lotcons , fe cluster(lottery)

su aig_diff 
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store C

xtreg lep_diff $outcomecats $lotcons , fe cluster(lottery) 

su lep_diff 
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store D

xtreg white_diff $outcomecats $lotcons , fe cluster(lottery)

su white_diff 
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store E

esttab main A B C D E using tableA11.tex , replace ///
mtitles("Avg Test Score" "School VA" "Ec Disad." "Ac. Gifted" "LEP" "White (pct)") ///
refcat(move1_lost "\emph{Lottery Loser}" move1_win_noncomp "\emph{Lottery Winner}" , nolabel ) ///
label booktabs nonum gaps f noobs collabels(none) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") star(* 0.10 ** 0.05 *** 0.01) keep(move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp) ///
coef(move1_lost "Change HS" notmove1_lost "Same HS" move1_win_noncomp "Non-Complier X Change HS"  notmove1_win_noncomp "Non-Complier X Same HS") 

esttab main A B C D E using tableA11.tex , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(mn sd, fmt(%10.3fc %10.3fc) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{(@)}") labels(`"Dep Var Mean"' `"Dep Var SD"'))
  
esttab main A B C D E using tableA11.tex , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(N_ind , fmt(%10.0fc) layout("\multicolumn{1}{c}{@}") labels(`"Observations"'))
  
 
 	
** sixth grade	
use all-lottery-movers-analysis.dta , clear 
keep if (year>2008 & sixsample & stayer==1)

gen frl_diff = frl_mb_at - frl_mb_as
gen white_diff = white_mb_at - white_mb_as
gen aig_diff = aig_at - aig_as
gen lep_diff = lep_Y_at - lep_Y_as
gen schoolva_diff = schoolva_at - schoolva_as
gen avgtestscores_at = (mathstand_at*0.5 + readstand_at*0.5)
gen avgtestscores_as = (mathstand_as*0.5 + readstand_as*0.5)
gen avgscore_diff = avgtestscores_at - avgtestscores_as

gen notmove1 = (1 - move1)
gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1
gen move1_lost = (1 - wonfirst)*move1
gen notmove1_lost = notmove1*(1 - wonfirst)
gen attappch1schl = (curr_lead1==appch1school)
gen attassschl = (curr_lead1==assschl)
gen win_complier = wonfirst*attassschl
gen win_noncomplier = wonfirst - win_complier
gen move1_win_noncomp = win_noncomplier*move1
gen notmove1_win_noncomp = win_noncomplier*notmove1

xtset lottery

global outcomecats "move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp"

xtreg avgscore_diff $outcomecats $lotcons $nccons , fe cluster(lottery) 

su avgscore_diff 
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store main

xtreg schoolva_diff $outcomecats $lotcons $nccons , fe cluster(lottery) 

su schoolva_diff 
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store A

xtreg frl_diff $outcomecats $lotcons $nccons , fe cluster(lottery)

su frl_diff 
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store B

xtreg aig_diff $outcomecats $lotcons $nccons , fe cluster(lottery)

su aig_diff 
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store C

xtreg lep_diff $outcomecats $lotcons $nccons , fe cluster(lottery) 

su lep_diff 
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store D

xtreg white_diff $outcomecats $lotcons $nccons , fe cluster(lottery)

su white_diff 
estadd scalar mn = r(mean)
estadd scalar sd = r(sd)

estadd scalar N_ind = e(N)

est store E

esttab main A B C D E using tableA12.tex , replace ///
mtitles("Avg Test Score" "School VA" "Ec Disad." "Ac. Gifted" "LEP" "White (pct)") ///
refcat(move1_lost "\emph{Lottery Loser}" move1_win_noncomp "\emph{Lottery Winner}" , nolabel ) ///
label booktabs nonum gaps f noobs collabels(none) ///
cells("b(fmt(3)star)" "se(fmt(3)par)") star(* 0.10 ** 0.05 *** 0.01) keep(move1_lost notmove1_lost move1_win_noncomp notmove1_win_noncomp) ///
coef(move1_lost "Change HS" notmove1_lost "Same HS" move1_win_noncomp "Non-Complier X Change HS"  notmove1_win_noncomp "Non-Complier X Same HS") 

esttab main A B C D E using tableA12.tex , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(mn sd, fmt(%10.3fc %10.3fc) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{(@)}") labels(`"Dep Var Mean"' `"Dep Var SD"'))
  
esttab main A B C D E using tableA12.tex , append ///
label booktabs nonum gaps f collabels(none) nomtitles plain cells(none) ///
stats(N_ind , fmt(%10.0fc) layout("\multicolumn{1}{c}{@}") labels(`"Observations"'))
 	
	
	
